﻿using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Audio;
using Terraria.ID;
using Terraria.ModLoader;

namespace HardcoreGame.Content.Projectiles.Ammo
{
    public class HellstoneSlingBullet : ModProjectile
    {
        public override void SetDefaults()
        {
            Projectile.friendly = true;         
            Projectile.hostile = false;
            Projectile.DamageType = DamageClass.Ranged;
            Projectile.penetrate = -1;
            Projectile.width = 14;
            Projectile.height = 14;
            Projectile.extraUpdates = 1;
            Projectile.ignoreWater = true;
        }

        public override Color? GetAlpha(Color lightColor)
        {
            return Color.White;
        }

        public override void AI()
        {
            Projectile.rotation += Projectile.velocity.X * 0.1f;
            Projectile.velocity.Y = Projectile.velocity.Y + 0.2f;

            if (Projectile.velocity.X > 18f)
            {
                Projectile.velocity.X = 18f;
            }
            if (Projectile.velocity.X < -18f)
            {
                Projectile.velocity.X = -18f;
            }

            if (Projectile.velocity.Y > 18f)
            {
                Projectile.velocity.Y = 18f;
            }
            if (Projectile.velocity.Y < -18f)
            {
                Projectile.velocity.Y = -18f;
            }

            if (Projectile.owner == Main.myPlayer && Projectile.timeLeft <= 3)
            {
                Projectile.tileCollide = false;
                Projectile.ai[1] = 0f;
                Projectile.alpha = 255;

                Projectile.position.X = Projectile.position.X + Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
                Projectile.width = 128;
                Projectile.height = 128;
                Projectile.position.X = Projectile.position.X - Projectile.width / 2;
                Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            }
            else
            {
                Lighting.AddLight(Projectile.position, 0.8f, 0.6f, 0f);
            }

            int num97 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 31, 0f, 0f, 100, default, 1.6f);
            Main.dust[num97].noGravity = true;
            num97 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 6, 0f, 0f, 100, default, 2f);
            Main.dust[num97].noGravity = true;
        }

        public override void ModifyHitNPC(NPC target, ref NPC.HitModifiers modifiers)
        {
            if (Main.expertMode)
            {
                if (target.type >= NPCID.EaterofWorldsHead && target.type <= NPCID.EaterofWorldsTail)
                {
                    modifiers.FinalDamage /= 5;
                }
            }
        }
        public bool canReduceTime = true;

        public override bool OnTileCollide(Vector2 oldVelocity) 
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
            }
            Projectile.velocity *= 0f;
            return false;
        }

        public override void OnHitNPC(NPC target, NPC.HitInfo hit, int damageDone)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }
            Projectile.usesLocalNPCImmunity = true;
            Projectile.localNPCImmunity[target.whoAmI] = 10;
            target.immune[Projectile.owner] = 0;

            if (Main.rand.Next(3) == 0)
            {
                target.AddBuff(BuffID.OnFire, 120, false);
            }
        }

        public override void OnHitPlayer(Player target, Player.HurtInfo info)
        {
            if (canReduceTime)
            {
                canReduceTime = false;
                Projectile.timeLeft = 2;
                Projectile.velocity *= 0f;
            }

            if (Main.rand.Next(3) == 0)
            {
                target.AddBuff(BuffID.OnFire, 120, false);
            }
        }

        public override void OnKill(int timeLeft)
        {
            SoundEngine.PlaySound(SoundID.Item14, Projectile.position);
            Projectile.position.X = Projectile.position.X + Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y + Projectile.height / 2;
            Projectile.width = 22;
            Projectile.height = 22;
            Projectile.position.X = Projectile.position.X - Projectile.width / 2;
            Projectile.position.Y = Projectile.position.Y - Projectile.height / 2;
            int num4;
            for (int num764 = 0; num764 < 20; num764 = num4 + 1)
            {
                int num765 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 31, 0f, 0f, 100, default, 1.5f);
                Dust dust = Main.dust[num765];
                dust.velocity *= 1.4f;
                num4 = num764;
            }
            for (int num766 = 0; num766 < 10; num766 = num4 + 1)
            {
                int num767 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 6, 0f, 0f, 100, default, 2.5f);
                Main.dust[num767].noGravity = true;
                Dust dust = Main.dust[num767];
                dust.velocity *= 5f;
                num767 = Dust.NewDust(new Vector2(Projectile.position.X, Projectile.position.Y), Projectile.width, Projectile.height, 6, 0f, 0f, 100, default, 1.5f);
                dust = Main.dust[num767];
                dust.velocity *= 3f;
                num4 = num766;
            }
            Vector2 position62 = new Vector2(Projectile.position.X, Projectile.position.Y);
            int num768 = Gore.NewGore(Projectile.GetSource_Death(), position62, default, Main.rand.Next(61, 64), 1f);
            Gore gore = Main.gore[num768];
            gore.velocity *= 0.4f;
            Gore gore138 = Main.gore[num768];
            gore138.velocity.X = gore138.velocity.X + 1f;
            Gore gore139 = Main.gore[num768];
            gore139.velocity.Y = gore139.velocity.Y + 1f;
            Vector2 position63 = new Vector2(Projectile.position.X, Projectile.position.Y);
            num768 = Gore.NewGore(Projectile.GetSource_Death(), position63, default, Main.rand.Next(61, 64), 1f);
            gore = Main.gore[num768];
            gore.velocity *= 0.4f;
            Gore gore140 = Main.gore[num768];
            gore140.velocity.X = gore140.velocity.X - 1f;
            Gore gore141 = Main.gore[num768];
            gore141.velocity.Y = gore141.velocity.Y + 1f;
            Vector2 position64 = new Vector2(Projectile.position.X, Projectile.position.Y);
            num768 = Gore.NewGore(Projectile.GetSource_Death(), position64, default, Main.rand.Next(61, 64), 1f);
            gore = Main.gore[num768];
            gore.velocity *= 0.4f;
            Gore gore142 = Main.gore[num768];
            gore142.velocity.X = gore142.velocity.X + 1f;
            Gore gore143 = Main.gore[num768];
            gore143.velocity.Y = gore143.velocity.Y - 1f;
            Vector2 position65 = new Vector2(Projectile.position.X, Projectile.position.Y);
            num768 = Gore.NewGore(Projectile.GetSource_Death(), position65, default, Main.rand.Next(61, 64), 1f);
            gore = Main.gore[num768];
            gore.velocity *= 0.4f;
            Gore gore144 = Main.gore[num768];
            gore144.velocity.X = gore144.velocity.X - 1f;
            Gore gore145 = Main.gore[num768];
            gore145.velocity.Y = gore145.velocity.Y - 1f;
        }
    }
}
